package com.example.toy.core.filter;

import com.example.toy.core.record.ToyRecord;
import com.example.toy.core.utils.SchemaHelper;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Objects;

/** 预定义各种类型的过滤器 */
public interface Filters {

  public static final AlwaysTrue ALWAYS_TRUE = new AlwaysTrue();

  public static final class AlwaysTrue implements ToyFilter {

    @Override
    public boolean accept(ToyRecord record) {
      return true;
    }
  }

  @Getter
  @RequiredArgsConstructor
  public static class EqualFilter implements ToyFilter {

    private final String field;
    private final Object value;

    @Override
    public boolean accept(ToyRecord record) {
      return Objects.equals(value, record.getFieldValue(field));
    }

    @Override
    public String toString() {
      return field + "=" + value;
    }
  }

  @Data
  public static class And implements ToyFilter {
    private final ToyFilter left;
    private final ToyFilter right;

    @Override
    public boolean accept(ToyRecord record) {
      return left.accept(record) && right.accept(record);
    }

    @Override
    public String toString() {
      return "(" + left + " and " + right + ")";
    }
  }

  @Data
  public static class Or implements ToyFilter {
    private final ToyFilter left;
    private final ToyFilter right;

    @Override
    public boolean accept(ToyRecord record) {
      return left.accept(record) || right.accept(record);
    }

    @Override
    public String toString() {
      return "(" + left + " or " + right + ")";
    }
  }

  @Data
  public static class Not implements ToyFilter {
    private final ToyFilter left;

    @Override
    public boolean accept(ToyRecord record) {
      return !left.accept(record);
    }

    @Override
    public String toString() {
      return "!(" + left + ")";
    }
  }
}
